<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <style>
        * {
            margin: 0;
            padding: 0;
            list-style: none;
        }

        body {
            background: #fff;
        }

        .btn {
            display: inline-block;
            padding: 4px 12px;
            margin-bottom: 0;
            font-size: 14px;
            line-height: 20px;
            text-align: center;
            vertical-align: middle;
            cursor: pointer;
            box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2), 0 1px 2px rgba(0, 0, 0, 0.05);
            border-radius: 4px;
        }

        .btn-danger {
            color: #fff;
            background-color: #da4f49;
            border: 1px solid #bd362f;
        }

        .btn-danger:hover {
            color: #fff;
            background-color: #bd362f;
        }

        .btn:focus {
            outline: none;
        }

        input:focus {
            box-shadow: 0px 0px 8px rgba(88, 170, 235, 0.8), inset 1px 3px 3px rgba(0, 0, 0, 0.03);
        }

        .todo-container {
            width: 600px;
            margin: 50px auto;
            padding: 15px;
            border: 1px solid #ddd;
            border-radius: 5px;
        }

        .todo-container .todo-header {
            margin-bottom: 20px;
        }

        .todo-container .todo-header input {
            width: 600px;
            box-sizing: border-box;
            height: 30px;
            padding: 0 10px;
            outline: none;
            border: 1px solid #ddd;
        }

        .todo-container .todo-header input:focus {
            outline: 0;
        }

        .todo-container .todo-main {
            border: 1px solid #ddd;
            border-radius: 5px;
        }

        .todo-container .todo-main .todo-list {
            height: 40px;
            line-height: 40px;
            padding: 0 5px;
            border-bottom: 1px solid #ddd;
        }

        .todo-container .todo-main .todo-list:last-child {
            border-bottom: none;
        }

        .todo-container .todo-main .todo-list button {
            float: right;
            margin-top: 3px;
        }

        .todo-container .todo-footer {
            height: 40px;
            line-height: 40px;
        }

        .todo-container .todo-footer button {
            float: right;
            margin-top: 5px;
        }
    </style>
    <title>表单练习</title>
</head>

<body>
    <div class="outer">
        <div class="todo-container">
            <!-- 头部 -->
            <div class="todo-header">
                <input type="text" id="newTodo" placeholder="请输入您的任务，回车确认">
            </div>
            <!-- 内容展示区域 -->
            <ul class="todo-main">
                <li class="todo-list">
                    <label>
                        <input type="checkbox">
                        <span>抽烟</span>
                    </label>
                    <button class="btn btn-danger">删除</button>
                </li>
                <li class="todo-list">
                    <label>
                        <input type="checkbox">
                        <span>喝酒</span>
                    </label>
                    <button class="btn btn-danger">删除</button>
                </li>
                <li class="todo-list">
                    <label>
                        <input type="checkbox">
                        <span>烫头发</span>
                    </label>
                    <button class="btn btn-danger">删除</button>
                </li>
            </ul>
            <!-- 底部区域 -->
            <div class="todo-footer">
                <label>
                    <input type="checkbox" id="checkAll">
                </label>
                <span>
                    已完成 <span id="allComplateTodos">0</span>
                    /全部 <span id="allTodos">0</span>
                </span>
                <button id="removeAllComplate" class="btn btn-danger">清除已完成任务</button>
            </div>
        </div>
    </div>
    <script>

        // -----------------------------------< 原生js操作 >----------------------------------------------
        // 数据库
        var data = [
            {
                content: "抽烟"
            },
            {
                content: "喝酒"
            },
            {
                content: "烫头"
            },
            {
                content: '打麻将'
            }
        ]
        // 修改数据库
        data.forEach(item => item.checked = false);
        let checkAll = document.querySelector('#checkAll');
        // 数据渲染视图
        renderDom(data);
        function renderDom(data) {
            let ulEL = document.querySelector('.todo-main');
            ulEL.innerHTML = '';
            data.forEach(function (item, index) {
                let liEl = document.createElement('li')
                liEl.classList.add('todo-list')
                liEl.innerHTML = `
                    <label>
                    <input type="checkbox" ${item.checked ? 'checked' : ''} >
                    <span>${item.content}</span>
                    </label>
                    <button class="btn btn-danger">删除</button>
                `
                // 绑定点击事件
                liEl.onclick = function (e) {
                    if (e.target.nodeName === 'INPUT') {
                        data[index].checked = e.target.checked;
                        num = 0;
                        data.forEach(item => {
                            num += item.checked;
                            if (num === data.length && data.length > 0) {
                                checkAll.checked = true;
                            } else {
                                checkAll.checked = false;
                            }
                            renderDom(data);
                        })
                    }
                    if (e.target.nodeName === 'BUTTON') {
                        data.splice(index, 1);
                        renderDom(data);
                        if (data.length === 0) {
                            checkAll.checked = '';
                        }
                    }
                }

                ulEL.appendChild(liEl);

                // 渲染底部全部
                document.querySelector('#allTodos').innerHTML = data.length;
                let num = 0;
                data.forEach(function (item) {
                    num += item.checked;
                })
                document.querySelector('#allComplateTodos').innerHTML = num;
            })
            // 头部添加功能实现
            let newTodo = document.querySelector('#newTodo');
            newTodo.addEventListener('keyup', function (e) {
                if (e.key === 'Enter') {
                    let newInfo = newTodo.value;
                    if (!newInfo.trim()) {
                        alert('老板别搞');
                        return
                    } else {
                        data.unshift({
                            content: newInfo,
                            checked: false
                        })
                    }
                    renderDom(data);
                    newTodo.value = '';
                }
            })

            // 底部功能实现
            document.querySelector('#checkAll').onclick = function (e) {
                let flag = e.target.checked;
                data.forEach(item => item.checked = flag);
                renderDom(data);
            }

            // 清除功能
            document.querySelector('#removeAllComplate').onclick = function () {
                console.log(111);
                data = data.filter(item => item.checked === false);
                if (data.length === 0) {
                    checkAll.checked = '';
                }
                renderDom(data);
            }

        }

    </script>
</body>